package cn.wp.ewallet.wallet.dao.impl;

import cn.wp.common.core.dao.JdbcSupportTemplate;
import cn.wp.common.core.dao.SqlString;
import cn.wp.common.core.model.WpPage;
import cn.wp.ewallet.wallet.dao.TransAccountLogDao;
import cn.wp.ewallet.wallet.extension.constant.EwalletWalletConstant;
import cn.wp.ewallet.wallet.model.dto.TransAccountLogReqDto;
import cn.wp.ewallet.wallet.model.entity.TransAccountLogDo;
import cn.wp.ewallet.wallet.model.entity.TransAccountLogDoo;
import cn.wp.ewallet.wallet.model.entity.TransAccountLogReqDoo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ClassName TransAccountLogDaoImpl
 * @Author ruixule
 * @Date 2020/1/7 10:28
 */
@Repository
public class TransAccountLogDaoImpl implements TransAccountLogDao {

    @Autowired
    private JdbcSupportTemplate jdbcSupportTemplate;

    /**
     * 将转账记录保存在数据库
     * @param transAccountLogDo
     * @return
     */
    @Override
    public int insertTransAccountLogDo(TransAccountLogDo transAccountLogDo) {
        return jdbcSupportTemplate.insert(transAccountLogDo);
    }

    /**
     * 通过钱包地址查询转账记录
     * @param walletAddress
     * @param moneyTypeId
     * @param page
     * @return
     */
    @Override
    public WpPage<TransAccountLogDo> selectTransAccountLogByWalletAdd(String walletAddress, String moneyTypeId, WpPage page) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select *");
        sqlString.add("from trans_account_log");
        sqlString.add("where money_type_id = ? and oper_type = ? ");
        sqlString.add("    and (address_from = ?");
        sqlString.add("        or address_to = ?)");
        sqlString.add(" order by trans_time desc");
        Object[] objects=new Object[]{
                moneyTypeId,
                EwalletWalletConstant.TRANS_LOG_TYPE_SYSTEM,
                walletAddress,
                walletAddress,
        };
        return jdbcSupportTemplate.selectList(sqlString.build(),objects,page,TransAccountLogDo.class);
    }

    @Override
    public WpPage<TransAccountLogDo> selectALLRecharge(String address, WpPage page) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select *");
        sqlString.add("from trans_account_log tl");
        sqlString.add("where tl.address_from is null");
        return jdbcSupportTemplate.selectList(sqlString.build(), new Object[]{}, page, TransAccountLogDo.class);
    }

    @Override
    public List<TransAccountLogDo> selectPendingList() {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select *");
        sqlString.add("from trans_account_log");
        sqlString.add("where oper_result = ?");
        sqlString.add("    and th_id is not null");

        return jdbcSupportTemplate.selectList(sqlString.build(), new Object[] {EwalletWalletConstant.TRANSFER_OPER_RESULT_WAITING}, TransAccountLogDo.class);
    }

    @Override
    public void updateTransAccountLog(TransAccountLogDo transAccountLogDo) {
        jdbcSupportTemplate.updateSelective(transAccountLogDo);
    }

    @Override
    public WpPage<TransAccountLogDoo> getCollectLog(TransAccountLogReqDoo transAccountLogReqDoo, WpPage wpPage) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select t.*, m.money_type_code, m.money_type_unit");
        sqlString.add("from trans_account_log t, money_type_info m");
        sqlString.add("where t.th_id is not null");
        sqlString.add("    and t.th_id != ''");
        sqlString.add("    and t.money_type_id = m.money_type_id");
        List<String> params = new ArrayList<>();
        if (StringUtils.isNotEmpty(transAccountLogReqDoo.getBeginDate())) {
            sqlString.add("    and t.trans_time > ?");
            params.add(transAccountLogReqDoo.getBeginDate());
        }
        if (StringUtils.isNotEmpty(transAccountLogReqDoo.getEndDate())) {
            sqlString.add("    and t.trans_time < ?");
            params.add(transAccountLogReqDoo.getEndDate());
        }
        if (StringUtils.isNotEmpty(transAccountLogReqDoo.getStatus())) {
            sqlString.add("    and t.oper_result = ?");
            params.add(transAccountLogReqDoo.getStatus());
        }
        if (StringUtils.isNotEmpty(transAccountLogReqDoo.getAddress())) {
            sqlString.add("    and (t.address_from = ?");
            sqlString.add("        or t.address_to = ?)");
            params.add(transAccountLogReqDoo.getAddress());
            params.add(transAccountLogReqDoo.getAddress());
        }
        sqlString.add("order by t.trans_time desc");

        return jdbcSupportTemplate.selectList(sqlString.build(), params.toArray(), wpPage, TransAccountLogDoo.class);
    }

    @Override
    public TransAccountLogDoo getLogById(String logId) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select t.*, m.money_type_code, m.money_type_unit");
        sqlString.add("from trans_account_log t, money_type_info m");
        sqlString.add("where t.money_type_id = m.money_type_id");
        sqlString.add("    and t.log_id = ?");
        return jdbcSupportTemplate.selectObject(sqlString.build(), new Object[] {logId}, TransAccountLogDoo.class);
    }

    /**
     * 查询系统内的转账记录  4000 表示系统内
     * @param wpPage
     * @return
     */
    @Override
    public WpPage<TransAccountLogDoo> getTransAccountLogsInsys(TransAccountLogReqDto transAccountLogReqDto, WpPage wpPage) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("SELECT a.*,b.user_tel from trans_account_log a");
        sqlString.add("  LEFT JOIN (");
        sqlString.add("select u.WALLET_ADDRESS, concat(p.mobile_prefix, '-', p.user_tel) as user_tel");
        sqlString.add(" from user_wallet_rel u, pa_user_info p");
        sqlString.add("where");
        sqlString.add("u.user_id = p.user_id  ) b");
        sqlString.add("on a.ADDRESS_FROM=b.WALLET_ADDRESS");
        sqlString.add("where a.OPER_TYPE = ?");
        List<String> listPara=new ArrayList<>();
        listPara.add(EwalletWalletConstant.TRANS_LOG_TYPE_SYSTEM);
        if ( StringUtils.isNotEmpty(transAccountLogReqDto.getBeginDate()) ){
            sqlString.add("and a.TRANS_TIME > ?");
            listPara.add(transAccountLogReqDto.getBeginDate());
        }
        if (StringUtils.isNotEmpty(transAccountLogReqDto.getEndDate())){
            sqlString.add("and a.TRANS_TIME < ?");
            listPara.add(transAccountLogReqDto.getEndDate());
        }
        if (StringUtils.isNotEmpty(transAccountLogReqDto.getStatus())){
            sqlString.add("and a.OPER_RESULT = ?");
            listPara.add(transAccountLogReqDto.getStatus());
        }
        sqlString.add(" order by a.TRANS_TIME desc");
        return jdbcSupportTemplate.selectList(sqlString.build(), listPara.toArray(),wpPage,TransAccountLogDoo.class);
    }

    @Override
    public List<TransAccountLogDo> selectYesterdayTransAccountLogList() {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select ifnull(m.service_charge,a.service_charge) as service_charge, a.money_type_id, a.oper_type, a.req_order_id");
        sqlString.add("from trans_account_log a");
        sqlString.add("left join mention_money_info m on m.req_order_id = a.req_order_id");
        sqlString.add("where a.oper_result = ? and a.trans_time >= ?");
        sqlString.add("    and a.trans_time <= ?");
        return jdbcSupportTemplate.selectList(sqlString.build(),new Object[]{EwalletWalletConstant.TRANSFER_OPER_RESULT_SUCCESS, getTodayZeroBeforeDaysTime(1),getTodayZeroBeforeDaysTime(0)},TransAccountLogDo.class);
    }
    //获取当前日期0点days之前的时间
    private Date getTodayZeroBeforeDaysTime(int days){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, -days*24);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }


}
